local InputManager = {}

local pointerX, pointerY = 0, 0
local previousX, previousY = 0, 0

function InputManager:initialize()

	self.keys = {
			a = 97,
			b = 98,
			c = 99,
			d = 100,
			e = 101,
			f = 102,
			g = 103,
			h = 104,
			i = 105,
			j = 106,
			k = 107,
			l = 108,
			m = 109,
			n = 110,
			o = 111,
			p = 112,
			q = 113,
			r = 114,
			s = 115,
			t = 116,
			u = 117,
			v = 118,
			w = 119,
			x = 120,
			y = 121,
			z = 122,
			kp8 = 56,
			kp2 = 50,
			kp4 = 52,
			kp6 = 54,
			zero = 48,
			one = 49,
			two = 50,
			three = 51,
			four = 52,
			five = 53,
			six = 54,
			seven = 55,
			eight = 56,
			nine = 57,
			plus = 43,
			minus = 45,
			tilde = 96,
			question = 47,
			colon = 59,
			space = 32,
			enter = 13,
			esc = 27,
			backspace = 8,
			r_sq_br = 93,
			l_sq_br = 91,
			
			
		}

	-- keyboard callback
	function onKeyboardEvent( key, down )
	
		--print(key, down)
		
		local command = self:convert_key( key )
		
		if Game.state == "gameplay" then
			Game:control_view(command, down)
		elseif Game.state == "menu" then
			if command == "save" then
			
				GS:gotoState('Scraper')
			
			end
		end
		
	end
	
	MOAIInputMgr.device.keyboard:setCallback ( onKeyboardEvent )
	
	if MOAIInputMgr.device.pointer then
		local pointerCallback = function (x, y)
			previousX, previousY = pointerX, pointerY
			pointerX, pointerY = x, y
			--print(x, y)
			if touchCallbackFunc then
				touchCallbackFunc( MOAITouchSensor.TOUCH_MOVE, 1,
					pointerX, pointerY, 1 )
			end
			
		end
		
		MOAIInputMgr.device.pointer:setCallback(pointerCallback)
		
		local button_manager = function (down)
			--require('mobdebug').on() -- zerobrane
		
			if down then
				print(self:position())
				local x, y = self:position()
				if HUD.d_maps then
					local map_num = HUD:select_map()
					if map_num then
						HUD:remove_map_display()
						MapManager:load_map( USER_NAME, map_num, 1 )
					else
					
						print("INPUT MANAGER load map invalid map")
					
					end
			
				end
				
				Game:check_clickable(x, y)
				
				
				
			end
			
			
		
		end
		
		MOAIInputMgr.device.mouseLeft:setCallback(button_manager)
	
	end
	
end

function InputManager:update_keys()
	local l_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.kp4)
	local r_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.kp6)
	local u_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.kp8)
	local d_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.kp2)
	local zi_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.r_sq_br)
	local zi_down2 = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.minus)
	local zo_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.l_sq_br)
	local zo_down2 = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.plus)
	local rr_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.nine)
	local rl_down = MOAIInputMgr.device.keyboard:keyIsDown(self.keys.seven)
	
	if l_down then
	
		Game:move_camera( 'left' )
		
	elseif r_down then
	
		Game:move_camera( 'right' )
		
	elseif u_down then
		Game:move_camera( 'up' )
	elseif d_down then
		Game:move_camera('down')
	elseif zi_down or zi_down2 then
		Game:move_camera('in')
	elseif zo_down or zo_down2 then
		Game:move_camera('out')
		
	elseif rr_down then
		Game:move_camera('rr')
		
	elseif rl_down then
		Game:move_camera('rl')
	end

end

function InputManager:convert_key ( key )

	if not HUD.d_maps and not HUD.console then
		if key == self.keys.nine then key = 'rot_right' end
		if key == self.keys.seven then key = 'rot_left' end
		if key == self.keys.kp8 then key = 'up' end
		if key == self.keys.kp2 then key = 'down' end
		if key == self.keys.kp4 then key = 'left' end
		if key == self.keys.kp6 then key = 'right' end
		if key == self.keys.r_sq_br then key = 'r_sq_br' end
		if key == self.keys.l_sq_br then key = 'l_sq_br' end
		
		if key == self.keys.e then key = 'waste' end
		if key == self.keys.l then key = 'load' end
		if key == self.keys.q then key = 'oxy' end
		if key == self.keys.r then key = 'water' end
		if key == self.keys.s then key = 'save' end
		if key == self.keys.t then key = 'supply' end
		if key == self.keys.w then key = 'wires' end
		
		if key == self.keys.tilde then key = 'tilde' end
		if key == self.keys.space then key = 'space' end
		if key == self.keys.enter then key = 'enter' end
		if key == self.keys.esc then key = 'esc' end
		if key == self.keys.backspace then key = 'backspace' end
	end
	
	if HUD.d_maps then
		if key == self.keys.one then key = 'one' end
		if key == self.keys.two then key = 'two' end
		if key == self.keys.three then key = 'three' end
		if key == self.keys.four then key = 'four' end
		if key == self.keys.five then key = 'five' end
		if key == self.keys.six then key = 'six' end
		if key == self.keys.seven then key = 'seven' end
		if key == self.keys.eight then key = 'eight' end
		if key == self.keys.nine then key = 'nine' end
	end
	
	if HUD.console then
	
		if key == self.keys.one then key = 'one' end
		if key == self.keys.two then key = 'two' end
		if key == self.keys.three then key = 'three' end
		if key == self.keys.four then key = 'four' end
		if key == self.keys.five then key = 'five' end
		if key == self.keys.six then key = 'six' end
		if key == self.keys.seven then key = 'seven' end
		if key == self.keys.eight then key = 'eight' end
		if key == self.keys.nine then key = 'nine' end
		if key == self.keys.a then key = 'a' end
		if key == self.keys.b then key = 'b' end
		if key == self.keys.c then key = 'c' end
		if key == self.keys.d then key = 'd' end
		if key == self.keys.e then key = 'e' end
		if key == self.keys.f then key = 'f' end
		if key == self.keys.g then key = 'g' end
		if key == self.keys.h then key = 'h' end
		if key == self.keys.i then key = 'i' end
		if key == self.keys.j then key = 'j' end
		if key == self.keys.k then key = 'k' end
		if key == self.keys.l then key = 'l' end
		if key == self.keys.m then key = 'm' end
		if key == self.keys.n then key = 'n' end
		if key == self.keys.o then key = 'o' end
		if key == self.keys.p then key = 'p' end
		if key == self.keys.q then key = 'q' end
		if key == self.keys.r then key = 'r' end
		if key == self.keys.s then key = 's' end
		if key == self.keys.t then key = 't' end
		if key == self.keys.u then key = 'u' end
		if key == self.keys.v then key = 'v' end
		if key == self.keys.w then key = 'w' end
		if key == self.keys.x then key = 'x' end
		if key == self.keys.y then key = 'y' end
		if key == self.keys.z then key = 'z' end
		if key == self.keys.tilde then key = 'tilde' end
		if key == self.keys.question then key = 'question' end
		if key == self.keys.colon then key = 'colon' end
		if key == self.keys.space then key = 'space' end
		if key == self.keys.enter then key = 'enter' end
		if key == self.keys.backspace then key = 'backspace' end
	end
		
	return key
end


function InputManager:position ()
	return pointerX, pointerY
end

function InputManager:previousPosition()
	return previousX, previousY
end

function InputManager:deltaPosition()
	return pointerX-previousX, previousY - pointerY
end

function InputManager:down ()

	if MOAIInputMgr.device.touch then
		return MOAIInputMgr.device.touch:down()
	elseif MOAIInputMgr.device.pointer then
		return ( MOAIInputMgr.device.mouseLeft:down() )
	end
	
end

function InputManager:isDown()
	-- queries both mouse and touch asking if we're clicking
	-- or touching
	if MOAIInputMgr.device.touch then
		
		return MOAIInputMgr.device.touch:isDown()
		
	elseif MOAIInputMgr.device.pointer then
	
		return (
			MOAIInputMgr.device.mouseLeft:isDown()
		)
		
	end
end

function InputManager:getTouch()

	if MOAIInputMgr.device.touch then
		return MOAIInputMgr.device.touch:getTouch()
	elseif MOAIInputMgr.device.pointer then
		return pointerX, pointerY, 1
	end
	
end



return InputManager















